<template>
  <div class="detail">
    <DetailNavBar ref="nav" @handleNavIndex="handleNavIndex" class="detail-nav" />
    <Scroll ref="scroll" class="content" :probeType="3" @scroll="handleScroll">
      <DetailSwiper :imgs="topImages" />
      <DetailBaseInfo :info="goods" />
      <DetailShopInfo :shopInfo="shop" />
      <DetailGoodsInfo :goodsInfo="detailInfo" @imgloaded="imgloaded" />
      <DetailParamInfo ref="param" :paramInfo="goodsParam" />
      <DetailCommendInfo ref="commend" :commends="commendInfo" />
      <GoodsList ref="recommend" :goods="recommends" />
    </Scroll>
    <DetailBottomBar @addToCart="handleAddToCart" />
    <BackTop @click.native="backclick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import DetailNavBar from "./children/DetailNavBar";
import DetailSwiper from "./children/DetailSwiper.vue";
import DetailBaseInfo from "./children/DetailBaseInfo.vue";
import DetailShopInfo from "./children/DetailShopInfo.vue";
import DetailGoodsInfo from "./children/DetailGoodsInfo.vue";
import DetailParamInfo from "./children/DetailParamInfo.vue";
import DetailCommendInfo from "./children/DetailCommendInfo.vue";
import DetailBottomBar from "./children/DetailBottomBar.vue";

import Scroll from "@/components/common/scroll/Scroll.vue";
import GoodsList from "@/components/content/goods/GoodsList.vue";
// import BackTop from "@/components/content/backTop/BackTop.vue";

import { itemListenerMixin, backTopMixin } from "@/common/mixin.js";
import { debounce } from "@/common/utils.js";

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend
} from "@/network/detail.js";

export default {
  name: "detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommendInfo,
    GoodsList,
    DetailBottomBar
    // BackTop
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      goodsParam: {},
      commendInfo: {},
      result: {},
      recommends: [],
      detailIOffsetTops: [],
      getTops: null,
      currentIndex: 0
      // isShowBackTop: false
      // refresh: {}
    };
  },
  created() {
    this.iid = this.$route.params.iid;

    getDetail(this.iid)
      .then(result => {
        this.result = result;
        const data = result.result;
        // 轮播图
        this.topImages = data.itemInfo.topImages;
        // 商品描述
        this.goods = new Goods(
          data.itemInfo,
          data.columns,
          data.shopInfo.services
        );
        // 店铺信息
        this.shop = new Shop(data.shopInfo);
        // 详情展示
        this.detailInfo = data.detailInfo;
        // 商品尺寸
        this.goodsParam = new GoodsParam(
          data.itemParams.info,
          data.itemParams.rule
        );
        // 商品评论
        if (data.rate.cRate !== 0) {
          this.commendInfo = data.rate.list[0];
        }

        // 渲染完成后获取位置,但是图片还没有加载完成
        // this.$nextTick(() => {
        //   this.detailIOffsetTops.push(0);
        //   this.detailIOffsetTops.push(this.$refs.param.$el.offsetTop);
        //   this.detailIOffsetTops.push(this.$refs.commend.$el.offsetTop);
        //   this.detailIOffsetTops.push(this.$refs.recommend.$el.offsetTop);
        //   console.log(this.detailIOffsetTops);
        // });
      })
      .catch(err => {});

    getRecommend()
      .then(result => {
        this.recommends = result.data.list;
      })
      .catch(err => {});

    this.getTops = debounce(() => {
      this.detailIOffsetTops.push(0);
      this.detailIOffsetTops.push(this.$refs.param.$el.offsetTop);
      this.detailIOffsetTops.push(this.$refs.commend.$el.offsetTop);
      this.detailIOffsetTops.push(this.$refs.recommend.$el.offsetTop);
      // console.log(this.detailIOffsetTops);
    }, 100);
  },
  methods: {
    imgloaded() {
      this.$refs.scroll.refresh();
      this.getTops();
    },
    handleNavIndex(index) {
      this.$refs.scroll.scrollTo(0, -this.detailIOffsetTops[index], 500);
    },
    handleScroll(pos) {
      let y = -pos.y;
      this.isShowBackTop = y > 800;
      for (let i = 0, len = this.detailIOffsetTops.length; i < len; i++) {
        if (this.currentIndex !== i) {
          if (
            (i < len - 1 &&
              y >= this.detailIOffsetTops[i] &&
              y < this.detailIOffsetTops[i + 1]) ||
            (i === len - 1 && y >= this.detailIOffsetTops[i])
          ) {
            this.currentIndex = i;
            this.$refs.nav.currentIndex = this.currentIndex;
          }
        }
      }
    },
    handleAddToCart() {
      // 创建商品信息
      const product = {};
      product.img = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      // 添加商品到购物车
      // this.$store.commit("addCartList", product);
      this.$store.dispatch("handleCart", product).then(res => {
        // this.$toast.methods.show(res);
        // console.log(this.$toast.show);
        this.$toast.show(res, 2000);
      });
    }
    // backclick() {
    //   this.$refs.scroll.scrollTo(0, 0, 500);
    // }
  },
  mounted() {
    // let newRefresh = debounce(this.$refs.scroll.refresh);
    // this.refresh = () => {
    //   newRefresh();
    // };
    // this.$bus.$on("goodsItemImgLoad", this.refresh);
  },
  updated() {
    // this.detailIOffsetTops.push(0);
    // this.detailIOffsetTops.push(this.$refs.param.$el.offsetTop);
    // this.detailIOffsetTops.push(this.$refs.commend.$el.offsetTop);
    // this.detailIOffsetTops.push(this.$refs.recommend.$el.offsetTop);
  },
  destroyed() {
    this.$bus.$off("goodsItemImgLoad", this.refresh);
  }
};
</script>

<style scoped>
.detail {
  background: #f6f6f6;
  height: 100vh;
  position: relative;
  z-index: 3;
}
.detail-nav {
  background: #fff;
  position: relative;
  z-index: 3;
}
.content {
  height: calc(100% - 93px);
}
</style>